//
// Created by zhang on 3/23/2023.
//
#include <stdio.h>
#include <stdlib.h>
#include "data_types.h"


typedef  struct StackNode {
    SElemType  data;
    struct StackNode *next;
} StackNode, *LinkStack;

void InitStack(LinkStack S) {
    S=NULL;
}

Status StackEmpty(LinkStack S) {
    if (S==NULL) return TRUE;
    else return FALSE;
}

Status Push(LinkStack *S, SElemType e) {
    StackNode * p= malloc(sizeof(StackNode));      //生成新结点p
    if (!p) exit(OVERFLOW);
    p->data=e;
    p->next=*S;
    *S=p;

    return OK;
}

Status Pop(LinkStack *S, SElemType *e) {
    StackNode *p;
    if (S==NULL) return ERROR;
    *e = (*S)->data;
    p = *S;
    *S = (*S)->next;
    free(p);

    return OK;
}

SElemType GetTop(LinkStack S) {
    if (S==NULL)  exit(ERROR);
    else  return S->data;
}

int main() {
    LinkStack S;

    InitStack(S);
    int i = 0;
    int val;

    for(i=0; i<10; i++) {
        Push(&S, i);
    }

    printf("%d\n", GetTop(S));

    for(i=0; i<10; i++) {
        Pop(&S, &val);
        printf("%d: %d\n", i, val);
    }

    return 0;
}

